package com.yunxi.service.localService.sys.impl;

import com.yunxi.core.model.CacheManager;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.sys.DictMapper;
import com.yunxi.model.sys.Dict;
import com.yunxi.service.localService.sys.DictService;
import com.yunxi.bizToBaseData.pojo.DictVO;
import com.yunxi.util.RedisCacheService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-08
 */
@Service
public class DictServiceImpl extends ServiceImplPlus<DictMapper, Dict> implements DictService {
   @Autowired
   DictMapper dictMapper;

   @Autowired
   RedisCacheService redisCacheService;

   public static CacheManager cacheManager   = CacheManager.getIntance("Thash");

   @Override
   public DictVO loadCas(String dictId) {
      return dictMapper.loadCas(dictId);
   }

   @Override
   public void saveBatchCas(DictVO dictVO) {
      Dict d = new Dict();
      BeanUtils.copyProperties(dictVO,d);
      redisCacheService.delete(dictVO.getDictKey());
      redisCacheService.deleteKeys("dicts::pms_dict_all");
      this.saveOrUpdate(d);
      //rcs.delete("dict:list_"+d.getDictId());
      //rcs.delete("dict:list_"+d.getPid());
      if (dictVO.getDictlist() != null && dictVO.getDictlist().size() > 0)
      {
         for (int i = 0; i < dictVO.getDictlist().size(); i++)
         {
            DictVO m = dictVO.getDictlist().get(i);
            Dict d2 = new Dict();
            BeanUtils.copyProperties(m,d2);
            d2.setPid(d.getDictId());
            saveOrUpdate(d2);
         }
      }
   }

   @Override
   public void delCas(List<DictVO> dictList) {
      if (dictList != null && dictList.size() > 0) {
         for (int i = 0; i < dictList.size(); i++) {
            DictVO m = dictList.get(i);
            baseMapper.deleteById(m.getDictId());
            baseMapper.deleteSub(m.getDictId());
         }
      }
   }

   @Override
   public List<DictVO> listByType(String type) {
      return baseMapper.listByType(type);
   }

   @Override
   public Map listAll() {
      Map map = (Map)cacheManager.getCache(".TSelect.pms_dict_all");
      if(map==null){
         List list = baseMapper.listAll(null);

         Map tempmap = new HashMap();
         for(int i=0;i<list.size();i++){
            DictVO  dict = (DictVO )list.get(i);
            tempmap.put(dict.getDictKey(),dict.getDictlist());
         }

         cacheManager.putCache(".TSelect.pms_dict_all",tempmap);
      }

      return map;
   }

   @Override
   public List listBySat() {
      return baseMapper.listBySat();
   }
}
